
$.ajaxSetup ({
   cache: false //关闭AJAX相应的缓存
});


//Loading dialog function
function ajaxLoading(){ 
    $("<div class=\"datagrid-mask\"></div>").css({display:"block",width:"100%"}).appendTo("body"); 
    $("<div class=\"datagrid-mask-msg\"></div>").html("正在处理，请稍候。。。").appendTo("body").css({display:"block",left:($(document.body).outerWidth(true) - 190) / 2,top:($(window).height() - 45) / 2}); 
} 
//Load end dialog function
function ajaxLoadEnd(){ 
     $(".datagrid-mask").remove(); 
     $(".datagrid-mask-msg").remove();             
}
//open loading dialog
ajaxLoading();


var pager;

$(function(){

	datagrid.datagridObj.pagination=true;

	datagrid.datagridObj.onSortColumn=function (sort, order) {
		paramData.sortOrder=order;
		paramData.sortColumn=sort;
		queryBtn_Filter();
        //alert("sort:"+sort+",order："+order+"");          
    };
    
	$('#'+datagrid.datagridId).datagrid(datagrid.datagridObj);
	
	
	pager=$('#'+datagrid.datagridId).datagrid('getPager');
	$(pager).pagination({
		pageSize:paramData.rows,
		pageList:[5,60,80,100,200],
		beforePageText: '第',//页数文本框前显示的汉字   
        afterPageText: '页    共 {pages} 页',   
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
        onSelectPage:function(pageNumber,pageSize){
	    	paramData.page = pageNumber;
	    	paramData.rows = pageSize; 
	    	reLoadData(); 
        }
	});
	
	
	//load end,show condition layout div
	$("#conditionNorth").css('visibility','visible');
	//load end ,close loading dialog 
	ajaxLoadEnd();
	//execute query
	queryBtn_Filter();


});

function addConditionFun(){
	for(var i=0; i<columnPropertyArr.length;i++){
		var key = columnPropertyArr[i];
		var type = columnPropertyTypeArr[i];
		var paramValue = '';
		if(type == 'date'){
			//DateBox 日期框
			paramValue=$("#"+key).datebox('getValue');
		}else if(type == 'combobox'){
			//select
			paramValue=$("#"+key).combobox('getValue');
		}else if(type == 'combotree'){
			//combotree
			paramValue=$("#"+key).combotree('getValue');
		}else{
			//Input
			paramValue = $("#"+key).textbox('getValue');//$("#"+key).val();
		}
		if(paramValue == ''){
			delete paramData[key];
			continue;
		}
		//后台提交参数
		paramData[key] = paramValue;
	}
	
	
}


function sortOrderFun_delete(order){
	paramData.sortOrder=order;
}

function reLoadData(){
	//$('#queryBtn_Filter').val('处理中..');
	$("#queryBtn_Filter").attr({"disabled":"disabled"});
	$('#'+datagrid.datagridId).datagrid('loading');//打开等待div  
	$.ajax({
		type:"POST",
		url:datagrid.datagridUrl,
		dataType:"json",
		data:paramData,
		success:function(msg){
			if(JSON.stringify(msg).indexOf('error:')==0){
            	window.parent.error(JSON.stringify(msg));
			}else{
				$('#list').datagrid('loadData',msg);
			}
			/*if(isIe){
				msgOut = msg;
				var msgNew = {};
				msgNew.total=msg.total-msg.rows.length;
				msgNew.rows=[];
				msgNew.footer=msg.footer;
				$('#'+datagrid.datagridId).datagrid('loadData',msgNew); 
			}else{
				$('#'+datagrid.datagridId).datagrid('loadData',msg); 
			}*/
		},
		error:function(er){
            window.parent.error(er);
	        $("#queryBtn_Filter").removeAttr("disabled"); 
	        $('#'+datagrid.datagridId).datagrid('loaded');
		}
	});
	
}

/*//查询
function queryBtn_Filter(){
	addConditionFun();
	/!*
	if($("#propertySortSelect_Filter").length>0){
		var sortColumn = $("#propertySortSelect_Filter").combobox('getValue');
		if(sortColumn=='0'){
			return ;
		}
		paramData.sortColumn = sortColumn;
		//paramData.page=1;
	}
	*!/
	paramData.page = 1;
	reLoadData();  
};*/


//查询
function queryBtn_Filter(){
	addConditionFun();
	paramData.page=1;
	$(pager).pagination('refresh',{pageNumber:paramData.page});
	reLoadData();
};

function datagrid_fresh(){
	$(pager).pagination('refresh',{pageNumber:paramData.page});
	reLoadData();
}


function clearCombotree(id){
	$('#'+id).combotree('clear');
}